/* memory.c
 *
 * implementations of kmalloc 
 */

#include "page.h"
#include "buddy.h"

/* function wrapper */
static void* __kmalloc_buddy_alloc(uint32 size);
static void __kmalloc_buddy_free_s(void* addr, uint32 size);

struct buddy_bucket kmalloc_bucket[] = {
     {4, 0, NULL},
     {8, 0, NULL},
     {16, 0, NULL},
     {32, 0, NULL},
     {64, 0, NULL},
     {128, 0, NULL},
     {256, 0, NULL},
     {512, 0, NULL},
     {1024, 0, NULL},
     {2048, 0, NULL}
};

struct buddy_class buddy_kmalloc_class = {
     .top_level_alloc = alloc_pages,
     .top_level_free_s = free_s_pages,
     .alloc = __kmalloc_buddy_alloc,
     .free_s = __kmalloc_buddy_free_s,
     .bucket = kmalloc_bucket,
     .bucket_total = 10,
};

static void* __kmalloc_buddy_alloc(uint32 size)
{
     return buddy_alloc(&buddy_kmalloc_class, size);
}

static void __kmalloc_buddy_free_s(void* addr, uint32 size)
{
     return buddy_free_s(&buddy_kmalloc_class, addr, size);
}

void* kmalloc(uint32 size)
{
     return __kmalloc_buddy_alloc(size);
}

void kfree_s(void* addr, uint32 size)
{
    __kmalloc_buddy_free_s(addr, size);
}

